
const GameDataManger = require("../../scripts/GameDataManger");
const UIPanelManager = require("../../scripts/UIPanelManager");


window.chessbox=new Array()

window.players = new Array();   //人员位置，0红放，1蓝方

window.roomId = 111;        //房间id

window.roomUserId = "7777";  //房主id

window.selfUserId = "111";  //自身id

window.selfData = null;  //自身数据

window.selfIndex = -1;  //自身位置坐标

window.gamestate='white'  //五子棋的状态  "white" 红方: "black"; 绿方

window.isPlaying = false;  //是否开始下棋

window.playIndex = 0;    //0 红方，1蓝方

window.playUserId = 11;    //下棋的UserId;

window.netState = 1;  //1未连接， 2链接中

window.isPlayingOver = false;  //是否主动结束游戏

window.remainTime = 20;  //倒计时

window.token = "";

window.connectNum = 0;

window.netUrl = "wss://wzqgame.247.hzguanqi.com/", //"ws://192.168.9.123:8001/"//

// 在 H5 游戏中监听 message 事件  
window.addEventListener('message', function(event) {  

    // if (event && event.origin !== "expected-origin-url") { // 安全性检查  
    //     return;  
    // }  
    const data = event.data;  
    if (data == 'closeSmallGame') {  
        cc.systemEvent.emit('sendMessage', 1013);
    }  
}); 
cc.Class({
    extends: cc.Component,

    properties: {

        whiteSpriteFrame:{//白棋的图片
            default:null,
            type:cc.SpriteFrame
        },
        chessman:{
            type:cc.Prefab,
            default:null,
        },
        blackSpriteFrame:{//黑棋的图片
            default:null,
            type:cc.SpriteFrame
        },

        chessmanBg:{
            type:cc.Prefab,
            default:null,
        },

        chesslist:{
            default:[],
            type:[cc.node]
        },
        chessbox:{
            type:[cc.node],
            default:[],
        },

        startOne:{
            type:cc.Node,
            default:null,
        },

        startTwo:{
            type:cc.Node,
            default:null,
        },

        juxuBtn:{
            type:cc.Node,
            default:null,
        },

        backBtn:{
            type:cc.Node,
            default:null,
        },

        closeBtn:{
            type:cc.Node,
            default:null,
        },

        //--------------------------------------------

        headleft:{
            type:cc.Node,
            default:null,
        },

        headRight:{
            type:cc.Node,
            default:null,
        },

        leftNameLab:{
            type:cc.Label,
            default:null,
        },

        rightNameLab:{
            type:cc.Label,
            default:null,
        },

        leftLeaveLBtn:{
            type:cc.Node,
            default:null,
        },

        rightLeaveLBtn:{
            type:cc.Node,
            default:null,
        },

        prossLine:{            //成功画线
            type:cc.Node,
            default:null,
        },

        //------------------------------------------------------

        loadView:{              //加载界面
            type:cc.Node,
            default:null,
        },

        loadTimeSp:{              //加载图标
            type:cc.Sprite,
            default:null,
        },

        loadTimeSpArr:{             //加载图标数组
            type:[cc.SpriteFrame],
            default:[],
        },

        //--------------------------------------------------------------

        resuletView:{          //结束界面
            type:cc.Node,
            default:null,
        },

        res_bgs:{          
            type:[cc.Node],
            default:[],
        },

        res_leftHead:{          
            type:cc.Node,
            default:null,
        },

        res_rightHead:{          
            type:cc.Node,
            default:null,
        },

        res_timeLab:{          
            type:cc.Label,
            default:null,
        },

        //-------------------------------------------------------------

        overView:{              //离开界面
            type:cc.Node,
            default:null,
        },

        nodeTip:{              //提示界面
            type:cc.Node,
            default:null,
        },

        quanNode:{              //圈
            type:cc.Node,
            default:null,
        },

        chaNode:{              //叉
            type:cc.Node,
            default:null,
        },

        //-------------------------------------------------------------

        chatBtn:{              //聊天按钮
            type:cc.Node,
            default:null,
        },

        chatNode:{              //聊天按钮
            type:cc.Node,
            default:null,
        },

        huiqiBtn:{              //悔棋
            type:cc.Node,
            default:null,
        },

        huiqiAiBtn:{             //悔棋人机
            type:cc.Node,
            default:null,
        },

        selfNameLab:{          
            type:cc.Label,
            default:null,
        },


        selfHead:{          
            type:cc.Node,
            default:null,
        },

        emojNode:{             //表情
            type:cc.Node,
            default:null,
        },

        emojNode2:{             //语言
            type:cc.Node,
            default:null,
        },


    },
    onLoad () {

        if (window.android){
            window.android.sendMessageToApp("enterGame")
        }
        //this.connect();

        this.over=cc.find("Canvas/gameView/background_1")
        this.test = cc.find("Canvas/gameView/test");

        this.main = cc.find("Canvas/main")
        this.gameView = cc.find("Canvas/gameView")

        this.main.active = true
        this.gameView.active = false; 

        for (let index = 0; index < 20; index++) {
            let emoj = this.emojNode.children[index];
            if (!emoj){
                emoj = cc.instantiate(this.emojNode.children[0])
                emoj.parent = this.emojNode
            }
            emoj.getComponent("emoj").setData(1,index+1);
        }

        for (let index = 0; index < GameDataManger.chatCoinfig.length; index++) {
            let emoj = this.emojNode2.children[index];
            if (!emoj){
                emoj = cc.instantiate(this.emojNode2.children[0])
                emoj.parent = this.emojNode2
            }
            emoj.getComponent("emoj").setData(2, GameDataManger.chatCoinfig[index]);
        }

        this.init();

        cc.systemEvent.on('sendMessage', this.sendMessage, this);
        cc.systemEvent.on('showTip', this.showTip, this);
        cc.systemEvent.on('ai_chess', this.ai_chess, this);
        cc.systemEvent.on('saveUserData', this.saveUserInfoData, this);
        cc.systemEvent.on('refresGame', this.refresGame, this);
        cc.systemEvent.on('closeChatNode', this.closeChatNode, this);
        cc.systemEvent.on('connectAi', this.connectAi, this);

        this.processURLParameters();

    },


    init(){
        players = [null, null];

        this.initInfoData();

        this.res_time = 10;      //倒计时

        this.dtTime = 0;

        this.closeBtn.active = false;
        this.startOne.active = false;
        this.startTwo.active = false;
        
        this.leftLeaveLBtn.active = false;
        this.rightLeaveLBtn.active = false;

        this.initBg();
        this.chessBoard()

        //todo 调试
        // isPlaying = true;
        this.changePlayer();
        
    },

    refresGame(){
        if (isPlaying){
            if (GameDataManger.gameType == 2){
                this.chatBtn.active = false;
                this.chatNode.active = false;
                this.huiqiBtn.active = false;
                this.huiqiAiBtn.active = true;
            }else{
                this.chatBtn.active = true;
                this.huiqiBtn.active = true;
                this.huiqiAiBtn.active = false;
            }
            //this.chatBtn.active = true;
        }
    },

    btnClickHandler(event, str){
        if (str == "playOne"){
            var ranIndex = Math.floor(Math.random()*2);
            ranIndex = 0
            this.sendMessage(1002, [ranIndex,true]);
        }
        else if (str == "playTwo"){
            var ranIndex = Math.floor(Math.random()*2); //先手，0红方，1绿方
            this.sendMessage(1002, [ranIndex,false]);
        }else if (str == "backStar"){
            this.sendMessage(1004);
        }else if (str == "jixuBtn"){
            this.overView.active = false;
        }else if (str == "closeBtn"){
            this.overView.active = true;
        }else if (str == "leaveLeft"){
            this.sendMessage(1006, [players[0].userId])
        }else if (str == "leaveRight"){
            this.sendMessage(1006, [players[1].userId])
        }else if (str == "closeRrsuleView"){
            this.resuletView.active = false;
        }
        else if (str == "leaveOne"){
            //单人认输
            this.sendMessage(1004, [selfUserId == roomUserId?1:0]);
        }
        else if (str == "huiqiAi"){
            //悔棋
            if (window.selfUserId != window.playUserId || gamestate=='stop'){
                cc.systemEvent.emit("showTip", "还未到你下棋的时候");
                return;
              }
              if(chessbox.length >= 2){
                this.regret_chess();
                this.regret_chess();
              }
        }
        else if (str == "huiqi"){
            //悔棋
            if (window.selfUserId != window.playUserId || gamestate=='stop'){
                cc.systemEvent.emit("showTip", "还未到你下棋的时候");
                return;
            }
            if (GameDataManger.gold < 10){
                cc.systemEvent.emit("showTip", "金币不够");
                return;
            }
            GameDataManger.gold-=10;
            this.saveUserInfoData();
            if(chessbox.length >= 2){
            this.regret_chess();
            this.regret_chess();
            }
        }
        else if (str == "chatBtn"){
            this.chatNode.active = true;
        }
    },

    closeChatNode(){
        this.chatNode.active = false;
    },

    //界面刷新，成员刷新
    changePlayer(){
        this.startOne.active = false;
        this.startTwo.active = false;
        
        this.leftLeaveLBtn.active = false;
        this.rightLeaveLBtn.active = false;

        this.chaNode.active = false;
        this.quanNode.active = false;

        window.selfIndex = -1;

        if (players[0] && players[0].userId == selfUserId){
            window.selfIndex = 0;
            selfData = players[0];
        }
        if (players[1] && players[1].userId == selfUserId){
            window.selfIndex = 1;
            selfData = players[1];
        }
        if(selfData){
            this.selfNameLab.string = selfData.name;
            this.selfHead.getComponent("head").refresh(0, true, selfData);
        }

        if (selfUserId == roomUserId && !isPlaying){
            if (players[0] && players[1]){
                this.startTwo.active = true;
            }
            else if (players[0] && !players[1]){
                    this.startOne.active = true; 
            }
            else if (players[1] && !players[0]){
                this.startOne.active = true; 
            }
            this.leftLeaveLBtn.active = (players[0])? true:false;

            this.rightLeaveLBtn.active = (players[1])? true:false;

        }else{
            if (!isPlaying){
                this.leftLeaveLBtn.active = window.selfIndex == 0;
    
                this.rightLeaveLBtn.active = window.selfIndex == 1;
            }
        }
        this.closeBtn.active = isPlaying && selfUserId == roomUserId;


        this.headleft.getComponent("head").refresh(0, true, players[0]);

        this.headRight.getComponent("head").refresh(0, false, players[1]);

        this.headleft.getComponent("head").setAddIcon(false);
        this.headRight.getComponent("head").setAddIcon(false);
        if (players[0]){
            this.leftNameLab.string = players[0].name;
        }else{
            this.leftNameLab.string = "";
            this.headleft.getComponent("head").setAddIcon(true);
        }

        if (players[1]){
            this.rightNameLab.string = players[1].name !=undefined ? players[1].name: "玩家";
        }else{
            this.rightNameLab.string = "box";
            this.headRight.getComponent("head").setAddIcon(true);
        }

        if (window.selfIndex == 0){
            this.chaNode.active = true;
        }else{
            this.quanNode.active = true;
        }

        this.main.active = !isPlaying;
        this.gameView.active = isPlaying; 

        this.refresGame();
    },

    //开始游戏
    beginStart(ranIndex){
        this.loadView.active = true;
        this.resuletView.active = false;
        this.restart();

        cc.tween(this.loadTimeSp)
        .call(()=>{
            this.loadTimeSp.spriteFrame = this.loadTimeSpArr[0];
        })
        .delay(1)
        .call(()=>{
            this.loadTimeSp.spriteFrame = this.loadTimeSpArr[1];
        })
        .delay(1)
        .call(()=>{
            if (selfIndex == -1 ){
                this.loadTimeSp.spriteFrame = this.loadTimeSpArr[4];
            }
            else if (selfIndex != ranIndex){
                this.loadTimeSp.spriteFrame = this.loadTimeSpArr[3];
            }else{
                this.loadTimeSp.spriteFrame = this.loadTimeSpArr[2];
            }
        })
        .delay(1)
        .call(()=>{
            //gamestate = ranIndex == 0 ? "white" : "black";
            this.loadView.active = false;
        })
        .start()
    },

    endStart(){
        this.overView.active = false;

        this.headleft.getComponent("head").refreshTime(0);
        this.headRight.getComponent("head").refreshTime(0);
    },

    restart:function(){
        var actionback=cc.moveTo(0.25,0,850)    //定义动作
        cc.find("Canvas/gameView/back/chessPar").destroyAllChildren()
        this.chessBoard()
        
        ////this.over.active = false;
        //cc.find("Canvas/gameView/background_1").runAction(actionback)
        cc.log("重置棋盘")
    },

    chessBoard:function(){
        this.chesslist = [];
        //将chess预制体铺满棋this.chessBoard()盘，然后点击时出发点击事件显示图片
    // for(var i=6;i>-6;i--)
    //  {
    //     for(var j=-5;j<7;j++)
    //     {
    for(var i=7;i>-8;i--)
        {
        for(var j=-7;j<8;j++)
        {
        //创建一个新的棋盘，将225个空棋子放入
        var newCode=cc.instantiate(this.chessman)
        cc.find("Canvas/gameView/back/chessPar").addChild(newCode)
        //newCode.setPosition(cc.v2(45*j,45*i))
        
        //鼠标监听事件，每次下棋时进行一次判断
        var self=this

        newCode.on(cc.Node.EventType.TOUCH_END,function(event)
        {

                
        });   
        
        newCode.getComponent("chesstrans").init(this.chesslist.length);
        this.chesslist.push(newCode)
        
        }
     }  
    
    },

    //获取上传棋子信息
    getQiziInfo(){
        var info = [];
        for (let index = 0; index < this.chesslist.length; index++) {
            const element = this.chesslist[index].getComponent("chesstrans");
            // if (element && element.getState() > 0){
            //     info[index] = element.getState();
            // }
            info.push(element.getState());
        }
        return info
    },

    //根据下发的棋子信息同步
    setQiziInfo(info, infoIndex = -1){
        for (let index = 0; index < this.chesslist.length; index++) {
            const element = this.chesslist[index].getComponent("chesstrans");
            if (info[index] && info[index] > 0){
                element.setState(info[index]);
            }

            if (index == infoIndex){
                element.setScaleBig();
            }
        }

        this.judgeOver()
    },

    initBg(){
        this.chessBgList = []
        var bgNum = 0;
        for(var i=7;i>-8;i--)
            {
               for(var j=-7;j<8;j++)
               {
                //格子初始话
                var newCodeBg=cc.instantiate(this.chessmanBg)
                cc.find("Canvas/gameView/back/bg").addChild(newCodeBg);

                this.chessBgList.push(newCodeBg);
                newCodeBg.children[0].active =  this.chessBgList.length%2 == bgNum;

                if (this.chessBgList.length%12 == 0){
                    bgNum = bgNum == 0? 1: 0;
                }
            }
        }
    },

    update (dt) {
        var self=this
        // if(isAi == true && gamestate=='black' && selfUserId == playUserId)
        // {
        //     //this.test.active=true
        //     setTimeout(function(){
        //     self.ai_chess()
        //     //self.test.active=false
            
        //     },500)
        //     gamestate='stop'
        // }

        //游戏结束时倒计时
        if (this.resuletView.active){
            this.dtTime += dt;
            if (this.res_time > 0 && this.dtTime >=1){
                this.dtTime = 0;
                this.res_time -= 1;
                this.res_timeLab.string = self.res_time.toString();
                if (this.res_time <= 0){
                    this.resuletView.active = false;
                }
            }
        }
    },

    //结束画线
    resultProssLine(node, rota, state){
        var self = this;
        this.prossLine.active = true;
        this.prossLine.setPosition(node.x + node.parent.x, node.y + node.parent.y)
        this.prossLine.angle = -rota

        this.prossLine.width = 0;

        isPlaying = false;      //比赛结束

        var wid = 200;
        if (rota == 45 || rota == -45){
            wid = 280;
        }
        cc.tween(self.prossLine)
        .to(1.5,{width: wid})
        .delay(1.5)
        .call(()=>{
            this.prossLine.active = false;
            self.judgeOverSend(state);
        })
        .start();
    },

    judgeOver:function(){
        var action=cc.moveTo(0.25,0,0)
        var actionback=cc.moveTo(0.25,0,850)    //定义动作
        cc.log("panduan")
        //判断行
        for(var i=0;i<11;i++)
        {
            for(var j=0;j<15;j++)
            {
               // cc.log(this.chesslist[112].getComponent(cc.Sprite).spriteFrame)
                if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame == this.whiteSpriteFrame &&
                this.chesslist[(i+1+j*15)].getComponent(cc.Sprite).spriteFrame == this.whiteSpriteFrame &&
                this.chesslist[(i+2+j*15)].getComponent(cc.Sprite).spriteFrame == this.whiteSpriteFrame 
                && this.chesslist[(i+3+j*15)].getComponent(cc.Sprite).spriteFrame == this.whiteSpriteFrame
                && this.chesslist[(i+4+j*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame)
                    {
                        this.resultProssLine(this.chesslist[(i+j*15)], 0, 0);
                    }
                else if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame == this.blackSpriteFrame &&
                    this.chesslist[(i+1+j*15)].getComponent(cc.Sprite).spriteFrame == this.blackSpriteFrame &&
                    this.chesslist[(i+2+j*15)].getComponent(cc.Sprite).spriteFrame == this.blackSpriteFrame 
                    && this.chesslist[(i+3+j*15)].getComponent(cc.Sprite).spriteFrame == this.blackSpriteFrame
                    && this.chesslist[(i+4+j*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame)
                    {
                       this.resultProssLine(this.chesslist[(i+j*15)], 0, 1);
                    }
               

                
            }
        }
        //判断列

        for(var i=0;i<15;i++)
        {
            for(var j=4;j<15;j++)
            {
                //判断是否有五个棋子连在一起

                if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                 && this.chesslist[(i+(j-1)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                 && this.chesslist[(i+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i+(j-4)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                {

                    this.resultProssLine(this.chesslist[(i+(j-4)*15)], 90, 0);
                }
                else if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                 && this.chesslist[(i+(j-1)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                 && this.chesslist[(i+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i+(j-4)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
               {
                    this.resultProssLine(this.chesslist[(i+(j-4)*15)], 90, 1);
               }
            }
        }
        //判断右斜方向
        for(var i=0;i<11;i++)
        {
            for(var j=4;j<15;j++)
            {
                if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                 && this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame  ==this.whiteSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame  ==this.whiteSpriteFrame
                 && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame  ==this.whiteSpriteFrame
                && this.chesslist[(i+4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame  ==this.whiteSpriteFrame) 
                {

                    this.resultProssLine(this.chesslist[(i+j*15)], -45, 0);
                    
                }
                else if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame  ==this.blackSpriteFrame
               && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame  ==this.blackSpriteFrame
                && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame  ==this.blackSpriteFrame
               && this.chesslist[(i+4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame  ==this.blackSpriteFrame)
               {
                    this.resultProssLine(this.chesslist[(i+j*15)], -45, 1);
               }

            }
        }
        //判断左斜方向
        for(var i=4;i<15;i++)
        {
            for(var j=4;j<15;j++)
            {
                if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                 && this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                 && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i-4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame) 
                {
                    this.resultProssLine(this.chesslist[(i-4+(j-4)*15)], 45, 0);
                }
                else if(this.chesslist[(i+j*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
               && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
               && this.chesslist[(i-4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame) 
               {
                    this.resultProssLine(this.chesslist[(i-4+(j-4)*15)], 45, 1);
               }

            }
        }

        let isheqi = true;
        for (let index = 0; index < this.chesslist.length; index++) {
            let element = this.chesslist[index];
            if (element.getComponent(cc.Sprite).spriteFrame == null){
                isheqi = false;
                break;
            }
        }
        if (isheqi){
            this.judgeOverSend(2);
        }

    },
    
    regret_chess:function(){
        
        var chessman=chessbox.pop()
        if (!chessman){
            return;
        }
        cc.log(chessman)
        chessman.getComponent("chesstrans").setState(0);

        // var chessmanx=chessman.node.position.y
        // var chessmany=chessman.node.position.x
        // var regret_chessman=(chessmany/60)+7+(7-chessmanx/60)*15
        // //出栈操作，获取最后一个入栈的节点，然后获取其position属性，并将其转换成为第几颗棋子

        // if(this.chesslist[regret_chessman].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
        // {
        //     //gamestate='white'
        // }
        // else
        // {
        //     //gamestate='black'
        // }
        // //判断最后一个是白棋还是黑棋，然后悔棋后仍然是该棋子下棋

        // this.chesslist[regret_chessman].getComponent(cc.Sprite).spriteFrame=null
        // //将该棋子变成空
    },

    ai_chess:function(){
        if (selfUserId != roomUserId){
            return;
        }
        if (!isPlaying){
            return
        }
        for(var m=0;m<255;m++)
       {
           chess_score[m]=0
           
               //四个
               //行
               for(var i=4;i<15;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-3+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-4+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               chess_score[i+j*15]+=50000
                           }
                           
                           
                       }
                       else if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-3+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-4+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }

                   }
               }
               for(var i=0;i<11;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+3+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+4+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               chess_score[i+j*15]+=50000
                           }
                           
                           
                       }
                       else if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+3+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+4+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }

                   }
               }
               //列
               for(var i=0;i<15;i++)
               {
                   for(var j=4;j<15;j++)
                   {
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-3)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-4)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50000
                           }
                       }
                       else if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-3)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-4)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }
                   }
               
               }
               for(var i=0;i<15;i++)
               {
                   for(var j=0;j<11;j++)
                   {
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+3)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+4)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50000
                           }
                       }
                       else if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+3)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+4)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }
                   }
               
               }
               //右斜
               for(var i=0;i<11;i++)
               {
                for(var j=4;j<15;j++)
               {
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i+4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=50000
                   }

                }
                else if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i+4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=60000
                   }
                }

               }
               }
               //
               for(var i=4;i<15;i++)
               {
                for(var j=0;j<11;j++)
               {
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i-3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i-4+(j+4)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame)
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=50000
                   }

                }
                else if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i-3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i-4+(j+4)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame)
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=60000
                   }
                }

               }
               }
               //左斜
               for(var i=4;i<15;i++)
               {
                   for(var j=4;j<15;j++)
                   {
                       if(
                        this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50000
                           }

                       }
                       else if(
                       this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-4+(j-4)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }
                   }
               }
               //
               for(var i=0;i<11;i++)
               {
                   for(var j=0;j<11;j++)
                   {
                       if(
                        this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+4+(j+4)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50000
                           }

                       }
                       else if(
                       this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+4+(j+4)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=60000
                           }
                       }
                   }
               }
               //三个
               //行
               for(var i=3;i<15;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-3+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i-4>0){
                                   chess_score[i+j*15]+=10000
                               }
                               else {
                                   chess_score[i+j*15]+=5000                                       
                           }
                           
                       }
                       else if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-3+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i-4>0){
                                   chess_score[i+j*15]+=15000
                               }
                               else{
                                   chess_score[i+j*15]+=7500
                               }

                           
                           }
                       }

                   }
               }
           }

               for(var i=0;i<12;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+3+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i+4<=11){
                                   chess_score[i+j*15]+=10000
                               }
                               else {
                                   chess_score[i+j*15]+=5000    
                              }
                           }
                           
                       }
                       else if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+3+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i+4<=11){
                                   chess_score[i+j*15]+=15000
                               }
                               else {
                                   chess_score[i+j*15]+=7500    
                           }
                       }

                   }
               }
           }
               //列
               for(var i=0;i<15;i++)
               {
                   for(var j=3;j<15;j++)
                   {
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-3)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(j-4>=0)
                               {
                                   chess_score[i+j*15]+=10000
                               }
                               else{
                                   chess_score[i+j*15]+=5000
                               }

                           
                           }
                       }
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-3)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(j-4>=0)
                               {
                                   chess_score[i+j*15]+=15000
                               }
                               else{
                                   chess_score[i+j*15]+=7500
                               }
                           }
                       }
                   }
               
               }
               //
               for(var i=0;i<15;i++)
               {
                   for(var j=0;j<12;j++)
                   {
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+3)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(j+4<=11)
                               {
                                   chess_score[i+j*15]+=10000
                               }
                               else{
                                   chess_score[i+j*15]+=5000
                               }
                           
                           }
                       }
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+3)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(j+4<=11)
                               {
                                   chess_score[i+j*15]+=15000
                               }
                               else{
                                   chess_score[i+j*15]+=7500
                               }
                           }
                       }
                   }
               
               }
               //右斜
               for(var i=0;i<12;i++)
               {
                for(var j=3;j<15;j++)
               {
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                       if(i+4<=11 && j-4>=0){
                           chess_score[i+j*15]+=10000
                       }
                       else{
                           chess_score[i+j*15]+=5000
                       }
                   
                   }

                }
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i+3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                       if(i+4<=11 && j-4>=0){
                           chess_score[i+j*15]+=15000
                       }
                       else{
                           chess_score[i+j*15]+=7500
                       }
                   }
                }

               }
               }

               for(var i=3;i<15;i++)
               {
                for(var j=0;j<12;j++)
               {
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                && this.chesslist[(i-3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                       if(i-4>=0 && j+4<=11){
                           chess_score[i+j*15]+=10000
                       }
                       else{
                           chess_score[i+j*15]+=5000
                       }
                   }

                }
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                && this.chesslist[(i-3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                       if(i-4>=0 && j+4<=11){
                           chess_score[i+j*15]+=15000
                       }
                       else{
                           chess_score[i+j*15]+=7500
                       }
                   }
                }

               }
               }
               //左斜
               for(var i=3;i<15;i++)
               {
                   for(var j=3;j<15;j++)
                   {
                       if(
                        this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i-4>=0 && j-4>=0){
                                   chess_score[i+j*15]+=10000
                               }
                               else{
                                   chess_score[i+j*15]+=5000
                               }
                           }

                       }
                       if(
                       this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-3+(j-3)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i-4>=0 && j-4>=0){
                                   chess_score[i+j*15]+=15000
                               }
                               else{
                                   chess_score[i+j*15]+=7500
                               }
                           }
                       }
                   }
               }
               //
               for(var i=0;i<12;i++)
               {
                   for(var j=0;j<12;j++)
                   {
                       if(
                        this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i+4<15 && j+4<15){
                                   chess_score[i+j*15]+=10000
                               }
                               else{
                                   chess_score[i+j*15]+=5000
                               }
                           }

                       }
                       if(
                       this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+3+(j+3)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                               if(i+4<15 && j+4<15){
                                   chess_score[i+j*15]+=15000
                               }
                               else{
                                   chess_score[i+j*15]+=7500
                               }
                           }
                       }
                   }
               }
               //两个
               //行
               for(var i=2;i<15;i++)
               {
                   for(var j=0;j<15;j++)
                   {      
                       if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame                           
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           }                            
                       }
                       else if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i-2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame                           
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               }
               //
               for(var i=0;i<13;i++)
               {
                   for(var j=0;j<15;j++)
                   {      
                       if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame                           
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           }                            
                       }
                       else if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+2+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame                           
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               }
               //列
               for(var i=0;i<15;i++)
               {
                   for(var j=2;j<15;j++)
                   {
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           } 
                       }
                       else if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j-2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               
               }

               //
               for(var i=0;i<15;i++)
               {
                   for(var j=0;j<13;j++)
                   {
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           } 
                       }
                       else if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       && this.chesslist[i+(j+2)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               
               }
               //右斜
               for(var i=0;i<13;i++)
               {
                for(var j=2;j<15;j++)
               {
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=2000
                   } 

                }
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i+2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=2500
                   } 
                }

               }
               }
               //
               for(var i=2;i<15;i++)
               {
                for(var j=0;j<13;j++)
               {
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=2000
                   } 

                }
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                && this.chesslist[(i-2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=2500
                   } 
                }

               }
               }
               //左斜
               for(var i=2;i<15;i++)
               {
                   for(var j=2;j<15;j++)
                   {
                       if(
                        this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           } 

                       }
                       if(
                       this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i-2+(j-2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               }
               //
               for(var i=0;i<13;i++)
               {
                   for(var j=0;j<13;j++)
                   {
                       if(
                        this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2000
                           } 

                       }
                       if(
                       this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       && this.chesslist[(i+2+(j+2)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=2500
                           } 
                       }
                   }
               }
               //一个
               //行
               for(var i=1;i<15;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                           
                       }
                       else if(this.chesslist[i-1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }

                   }
               }
               //
               for(var i=0;i<14;i++)
               {
                   for(var j=0;j<15;j++)
                   {
                       
                       if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                           
                       }
                       else if(this.chesslist[i+1+j*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }

                   }
               }
               //列
               for(var i=0;i<15;i++)
               {
                   for(var j=1;j<15;j++)
                   {
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                       }
                       if(this.chesslist[i+(j-1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }
                   }
               
               }
               //
               for(var i=0;i<15;i++)
               {
                   for(var j=0;j<14;j++)
                   {
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.whiteSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                       }
                       if(this.chesslist[i+(j+1)*15].getComponent(cc.Sprite).spriteFrame==this.blackSpriteFrame
                       
                       
                       )
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }
                   }
               
               }
               //右斜
               for(var i=0;i<14;i++)
               {
                for(var j=1;j<15;j++)
               {
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=20
                   } 

                }
                if(this.chesslist[(i+1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=50
                   } 
                }

               }
               }
               //
               for(var i=1;i<15;i++)
               {
                for(var j=0;j<14;j++)
               {
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=20
                   } 

                }
                if(this.chesslist[(i-1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                
                
                )
                {
                   if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                   {
                   chess_score[i+j*15]+=50
                   } 
                }

               }
               }
               //左斜
               for(var i=1;i<15;i++)
               {
                   for(var j=1;j<15;j++)
                   {
                       if(
                        this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        
                        
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                       }
                       if(
                       this.chesslist[(i-1+(j-1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       
                       
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }
                   }
               }   
               //
               for(var i=0;i<14;i++)
               {
                   for(var j=0;j<14;j++)
                   {
                       if(
                        this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.whiteSpriteFrame
                        
                        
                        ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=20
                           } 
                       }
                       if(
                       this.chesslist[(i+1+(j+1)*15)].getComponent(cc.Sprite).spriteFrame ==this.blackSpriteFrame
                       
                       
                       ) 
                       {
                           if(this.chesslist[i+j*15].getComponent(cc.Sprite).spriteFrame==null)
                           {
                           chess_score[i+j*15]+=50
                           } 
                       }
                   }
               } 
       }

       var MAX=0
       var position=0
       for(var k=0;k<224;k++)
       {
           if(chess_score[k]>MAX)
           {
               MAX=chess_score[k]
               position=(function(x){
                   return x
               }(k))
           }
       }
       let info = this.getQiziInfo()
       if (Object.keys(info).length === 0 && info.constructor === Object){
            position = 112;
       }
       if(this.chesslist[position].getComponent(cc.Sprite).spriteFrame==null)
       {
        let nowState = gamestate;
        if (gamestate =='stop') {
            return;
        }
        gamestate ='stop'
        this.chesslist[position].getComponent("chesstrans").setAnimaState(playIndex+1);
        setTimeout(() => {
            this.chesslist[position].getComponent(cc.Sprite).spriteFrame = nowState == "white" ? this.whiteSpriteFrame : this.blackSpriteFrame
            cc.log(position)
            this.sendMessage(1005, [position]);
          },500);
          
       }
       //this.judgeOver()
       chessbox.push(this.chesslist[position]) //进栈

},

    showTip(str, type = 0){
        type = 0;
        this.nodeTip.children[0].getComponent(cc.Label).string = str
        if (this.tipTween){
            this.tipTween.stop();
        }
        this.tipTween = cc.tween(this.nodeTip)
            .to(0.1,{scale: 1})
            .delay(1)
            .to(0.1,{scale: type == 0 ? 0 : 1})
            .start()
    },


    //请求 结果通知 0红胜， 1红负， 2平 
    judgeOverSend(state){
        isPlaying = false;
        if (selfUserId == roomUserId){
            this.sendMessage(1003, [state]);
        }
    },

    //结果通知 0红胜， 1红负， 2平 
    onJudgeOverSend(state){
        this.dtTime = 1;
        this.res_time = 11;

        //this.resuletView.active = true;
        this.res_bgs[0].active = false;     //胜利
        this.res_bgs[1].active = false;     //失败
        this.res_bgs[2].active = false;     //平
        if (state == 2){
            this.res_bgs[2].active = true;
        }
        else if (selfIndex == -1){
            if(state == 2){

            }
            this.res_bgs[0].active = true;
        }else{
            if (state == 0 && selfIndex == 0){
                this.res_bgs[0].active = true;
            }else if (state == 1 && selfIndex == 1){
                this.res_bgs[0].active = true;
            }else{
                this.res_bgs[1].active = true;
            }
        }

        if(this.res_bgs[0].active){
            GameDataManger.resultNum = 1
        }else{
            GameDataManger.resultNum = 2
        }
        console.log("游戏结果：" + GameDataManger.resultNum)
        UIPanelManager.open(UIPanelManager.resultNode);

        this.res_leftHead.getComponent("head").refresh(1, true, players[0], state);

        this.res_rightHead.getComponent("head").refresh(1, false, players[1], state);

    },

    processURLParameters() {  
        let url = window.location.href;  

        //url = "http://111.230.61.90:9222/?userId=100000627&roomId=st860769849&token=9c64ed795ee040bcac01c3ba96c6d589&ownerId=100000617";

        // 提取查询字符串部分  
        let queryString = url.split('?')[1]; // ['api.coincraze.ai/api/user', 'user_id=1111111']  
  
        // 解析查询字符串  
        let params = {};  
        if (queryString){
            queryString.split('&').forEach(function(pair) {  
                let keyValue = pair.split('=');  
                let key = decodeURIComponent(keyValue[0]);  
                let value = decodeURIComponent(keyValue[1] || ''); // 如果没有=号，则value为空字符串  
                params[key] = value;  
            });  
        }
        // 从解析后的参数中获取user_id  
        selfUserId = params['userId'];
        roomId = params['roomId'];
        roomUserId = params['ownerId'];
        token = params['token'];  
        if (selfUserId == undefined){
            selfUserId = 1// "1a909829441a42dda8ed1d82ad9a8416";
            roomId = 456;
        }
        roomId = 456;
        roomUserId = 1;
        // if(CC_PREVIEW){
        //     selfUserId = 3
        //     roomId = 567;
        //     roomUserId = 3;
        // }
        selfUserId = selfUserId.toString();
        connectNum = 0
        //this.connect();
    },

    connectAi(lev){
        var self = this;
        // 初始化 WebSocket 连接  
        lev = lev+1
        console.log("难度等级：" + lev);
        this.webSocketAi = new WebSocket( netUrl + 'ai/' + lev + "/" +selfUserId +"/");  //"&token=" + token+ 
        //this.webSocket = new WebSocket('wss://150.109.148.67:9204/message/' + "?roomId=" + roomId + "&userId=" + selfUserId + "&token=" + token+ "&ownerId=" + roomUserId);  
        // WebSocket 连接打开时的回调  
        this.webSocketAi.onopen = (event) => {  
            console.log('WebSocketAi connection opened', event);  
            // 连接打开后，可以发送消息  
            //this.webSocket.send('Hello Server!');  
            netState = 2;
            connectNum = 0
        }; 

         // WebSocket 连接关闭时的回调  
         this.webSocketAi.onclose = (event) => {  
            console.log('WebSocketAi connection closed', event);  
            // netState = 1;
            // connectNum++;
            // if (connectNum < 3){
            //     this.connect();
            // }else{
            //     this.showTip("网络断开，请重新登录", 1)
            //     this.sendReloadGame();
            // }
        };  

        // WebSocket 接收到消息时的回调  
        this.webSocketAi.onmessage = (event) => {  
            console.log('WebSocketAi message received', event.data);  
            // 可以在这里处理接收到的消息 
            var data = JSON.parse(event.data); 
            var protoId = data.protoId;
            if(protoId == 1001){
                players = data.players;
                roomUserId = data.roomUserId;
                this.changePlayer();

                cc.systemEvent.emit('refreshMatch')
                //this.judgeOverSend(1);
            }
            else if(protoId == 1002){
                isPlaying = true;
                isPlayingOver = false;
                this.beginStart(data.index);

                this.changePlayer();
            }else if (protoId == 1003){         //下棋结束
                this.setitem
                isPlaying = false;
                this.onJudgeOverSend(data.status)
                this.endStart();

                this.changePlayer();
                this.closeWebSocketAi();
            }
            else if (protoId == 1004){          //房主结束
                isPlaying = false;
                isPlayingOver = true;
                this.endStart();

                this.changePlayer();
                this.closeWebSocketAi();
            }
            else if (protoId == 1005){
                this.setQiziInfo(data.info, data.infoIndex);
            }
            else if (protoId == 1008){          //通知该谁下棋了
                if (isPlaying){
                    playIndex = data.index;
                    playUserId = data.userId;
                    gamestate = playIndex == 0 ? 'white' : "black";

                    if (data.isAi == "true"){
                        // this.headleft.getComponent("head").stopTime()
                        // this.headRight.getComponent("head").stopTime()
                        // self.ai_chess()
                    }else{
                        setTimeout(() => {
                            if (isPlaying){
                                if (playIndex == 0){
                                    this.headleft.getComponent("head").refreshTime(20);
                                    this.headRight.getComponent("head").stopTime()
                                }else{
                                    this.headleft.getComponent("head").stopTime()
                                    this.headRight.getComponent("head").refreshTime(20);
                                }
                            }
                        },  this.loadView.active ? 4000 : 10);
                    }  
                }
            }
            else if (protoId == 1009){
                if (roomUserId == data.userId){
                    //this.showTip("房主已上线")
                }
            }
            else if (protoId == 1010){
                if (roomUserId == data.userId){
                    //this.showTip("房主已离开，请等待房主重连~", 1)
                    this.showTip("房间已被解散", 1)
                }
            }
            else if (protoId == 1011){
                return;
            }
            else if (protoId == 1012){
                isPlaying = data.is_play == "true";

                this.changePlayer();
            }
            else if (protoId == 1013){
                this.showTip("房间已被解散", 1)
                this.changePlayer();
            }
            else if (protoId == 1022){
                //表情下发
                if(isPlaying){
                    this.headleft.getComponent("head").showEmoj(data)
                    this.headRight.getComponent("head").showEmoj(data)
                }
            }
            if (!isPlaying){
                this.headleft.getComponent("head").stopTime()
                this.headRight.getComponent("head").stopTime()
            }
            
        };  
  
        // WebSocket 发生错误时的回调  
        this.webSocketAi.onerror = (error) => {  
            console.log('WebSocketAi error', error);  

            //this.showTip("网络错误，请重新登录")
            //this.sendReloadGame();
        };  
    },


    connect() {  
        var self = this;
        // 初始化 WebSocket 连接  
        this.webSocket = new WebSocket( netUrl + 'message/' + "?roomId=" + roomId + "&userId=" + selfUserId + "&ownerId=" + roomUserId);  //"&token=" + token+ 
        //this.webSocket = new WebSocket('wss://150.109.148.67:9204/message/' + "?roomId=" + roomId + "&userId=" + selfUserId + "&token=" + token+ "&ownerId=" + roomUserId);  
        // WebSocket 连接打开时的回调  
        this.webSocket.onopen = (event) => {  
            console.log('WebSocket connection opened', event);  
            // 连接打开后，可以发送消息  
            //this.webSocket.send('Hello Server!');  
            netState = 2;
            connectNum = 0
        };  
  
        // WebSocket 接收到消息时的回调  
        this.webSocket.onmessage = (event) => {  
            console.log('WebSocket message received', event.data);  
            // 可以在这里处理接收到的消息 
            var data = JSON.parse(event.data); 
            var protoId = data.protoId;
            if(protoId == 1001){
                players = data.players;
                roomUserId = data.roomUserId;
                this.changePlayer();

                cc.systemEvent.emit('refreshMatch')
                //this.judgeOverSend(1);
            }
            else if(protoId == 1002){
                isPlaying = true;
                isPlayingOver = false;
                this.beginStart(data.index);

                this.changePlayer();
            }else if (protoId == 1003){         //下棋结束
                isPlaying = false;
                this.onJudgeOverSend(data.status)
                this.endStart();

                this.changePlayer();
            }
            else if (protoId == 1004){          //房主结束
                isPlaying = false;
                isPlayingOver = true;
                this.endStart();

                this.changePlayer();
            }
            else if (protoId == 1005){
                this.setQiziInfo(data.info, data.infoIndex);
            }
            else if (protoId == 1008){          //通知该谁下棋了
                if (isPlaying){
                    playIndex = data.index;
                    playUserId = data.userId;
                    gamestate = playIndex == 0 ? 'white' : "black";

                    if (data.isAi == "true"){
                        this.headleft.getComponent("head").stopTime()
                        this.headRight.getComponent("head").stopTime()
                        self.ai_chess()
                    }else{
                        setTimeout(() => {
                            if (isPlaying){
                                if (playIndex == 0){
                                    this.headleft.getComponent("head").refreshTime(20);
                                    this.headRight.getComponent("head").stopTime()
                                }else{
                                    this.headleft.getComponent("head").stopTime()
                                    this.headRight.getComponent("head").refreshTime(20);
                                }
                            }
                        },  this.loadView.active ? 4000 : 10);
                    }  
                }
            }
            else if (protoId == 1009){
                if (roomUserId == data.userId){
                    //this.showTip("房主已上线")
                }
            }
            else if (protoId == 1010){
                if (roomUserId == data.userId){
                    //this.showTip("房主已离开，请等待房主重连~", 1)
                    this.showTip("房间已被解散", 1)
                }
            }
            else if (protoId == 1011){
                return;
            }
            else if (protoId == 1012){
                isPlaying = data.is_play == "true";

                this.changePlayer();
            }
            else if (protoId == 1013){
                this.showTip("房间已被解散", 1)
                this.changePlayer();
            }
            else if (protoId == 1022){
                //表情下发
                if(isPlaying){
                    this.headleft.getComponent("head").showEmoj(data)
                    this.headRight.getComponent("head").showEmoj(data)
                }
            }
            if (!isPlaying){
                this.headleft.getComponent("head").stopTime()
                this.headRight.getComponent("head").stopTime()
            }
            
        };  
  
        // WebSocket 连接关闭时的回调  
        this.webSocket.onclose = (event) => {  
            console.log('WebSocket connection closed', event);  
            netState = 1;
            connectNum++;
            if (connectNum < 3){
                this.connect();
            }else{
                this.showTip("网络断开，请重新登录", 1)
                this.sendReloadGame();
            }
        };  
  
        // WebSocket 发生错误时的回调  
        this.webSocket.onerror = (error) => {  
            console.error('WebSocket error', error);  

            this.showTip("网络错误，请重新登录")
            //this.sendReloadGame();
        };  
    },  
  
    onDestroy() {  
        // 销毁时关闭 WebSocket 连接  
        if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {  
            this.webSocket.close();  
        }  
    },  

    //关闭socketAi
    closeWebSocketAi(){
        if (this.webSocketAi && this.webSocketAi.readyState === WebSocket.OPEN) {  
            this.webSocketAi.close();  
        }  
    },
  
    // 你可以添加一个方法来发送消息  
    sendMessage(protoId, data) {  
        var message = {}
        message.protoId = protoId
        if (protoId == 1002){           //开始下棋
            message.index = data[0];
            message.isAi = data[1];
        }
        else if (protoId == 1003){           //下棋结束 正常
            message.status = data[0];
        }
        else if (protoId == 1004){           //下棋结束 房主主动 认输
            //isPlayingOver = true;
            message.status = data[0];
        }
        else if (protoId == 1005){      //棋子信息同步
            message.index = playIndex;
            message.info = this.getQiziInfo();
            message.infoIndex = data[0];

            this.headleft.getComponent("head").stopTime()
            this.headRight.getComponent("head").stopTime()
        }
        else if (protoId == 1006){      //踢人
            message.userId = data[0];
        }
        else if (protoId == 1007){      //参加比赛
            message.userId = selfUserId;
            message.index = data[0];
        }
        else if (protoId == 1013){      //房间解散

        }
        else if (protoId == 1021){      //跟新匹配状态
            message.userId = selfUserId;
            message.status = data[0];
        }
        else if (protoId == 1022){      //发送表情
            message.userId = selfUserId;
            message.id = data[0];
        }
        var roomProIds = [1002, 1003];
        if (roomProIds.indexOf(protoId) >= 0 && selfUserId != roomUserId){
            console.log("非房主无法发送 sendMessage:" + JSON.stringify(message))
            return;
        }

        var roomUserProIds = [1005, 1003];
        if (isPlayingOver && roomUserProIds.indexOf(protoId) >= 0){
            console.log("主动结束后，部分协议不再发送")
            return;
        }

        console.log("sendMessage:" + JSON.stringify(message))
        if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {  
            this.webSocket.send(JSON.stringify(message));  
        } else {  
            //console.log('WebSocket is not open'); 
            //this.showTip("网络断开，请重新登录") 
        }  

        if (this.webSocketAi && this.webSocketAi.readyState === WebSocket.OPEN) {  
            this.webSocketAi.send(JSON.stringify(message));  
        } else {  
            // console.error('WebSocket is not open'); 
            // this.showTip("网络断开，请重新登录") 
        }  
    }, 

    sendReloadGame(){
        if (window.android){
            window.android.sendMessageToApp("reloadGame")
        }
    },

    //获取缓存数据
    initInfoData(){
        let data = cc.sys.localStorage.getItem("UserInfoData");
        if (data){
            data = JSON.parse(data)

            GameDataManger.musicVal = data.musicVal
            GameDataManger.soundVal = data.soundVal
            GameDataManger.signDay = data.signDay
            GameDataManger.signTime = data.signTime
            GameDataManger.gold = data.gold
            GameDataManger.curLevel = data.curLevel
            GameDataManger.selfScore = data.selfScore
        }
    },

    //保存数据
    saveUserInfoData(){
        let user = {
            "musicVal":GameDataManger.musicVal,
            "soundVal":GameDataManger.soundVal,
            "signDay":GameDataManger.signDay,
            "signTime":GameDataManger.signTime,
            "gold":GameDataManger.gold,
            "curLevel":GameDataManger.curLevel,
            "selfScore":GameDataManger.selfScore,
        }

        cc.sys.localStorage.setItem("UserInfoData", JSON.stringify(user));

        cc.systemEvent.emit('refresGame');
    },

});
